package com.supreme.omsserver.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.benmanes.caffeine.cache.Cache;
import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.omsserver.entity.mysql.User;
import com.supreme.omsserver.mapper.UserMapper;
import com.supreme.omsserver.service.UserService;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "user")
public class UserController {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserService userService;

    @Resource
    private Cache<String, List<User>> cacheUserList;

    @GetMapping(value = "truncate")
    public AjaxResp truncate() {
        userMapper.truncateTable();
        return AjaxResp.instance();
    }

    @PostMapping(value = "insert")
    public AjaxResp insert() {
        String username = RandomStringUtils.randomAlphabetic(16);
        String userCode = RandomStringUtils.randomNumeric(8);
        int gender = RandomUtils.nextInt(1, 2);
        User user = new User()
                .setUsername(username)
                .setUserCode(userCode)
                .setGender(gender)
                .setBirthday(new Date());
        int insert = userMapper.insert(user);
        return AjaxResp.instance(insert);
    }

    @PostMapping(value = "insertList")
    public AjaxResp insertList(@RequestParam(value = "batchCount", defaultValue = "100") Integer batchCount) {
        List<User> userList = new ArrayList<>(batchCount);
        for (int i = 0; i < batchCount; i++) {
            String username = RandomStringUtils.randomAlphabetic(16);
            String userCode = RandomStringUtils.randomNumeric(8);
            int gender = RandomUtils.nextInt(1, 2);
            User user = new User()
                    .setUsername(username)
                    .setUserCode(userCode)
                    .setGender(gender)
                    .setBirthday(new Date());
            userList.add(user);
        }
        userService.saveBatch(userList);
        return AjaxResp.instance(userList);
    }

    @GetMapping(value = "selectList")
    public AjaxResp selectList() {
        List<User> userList = userService.list();
        Thread thread = Thread.currentThread();
        System.out.println("thread.getName() = " + thread.getName());
        return AjaxResp.instance(userList);
    }

    @GetMapping(value = "selectOne")
    public AjaxResp selectOne(User user) {
        User one = userService.getOne(Wrappers.lambdaQuery(User.class).eq(User::getUserId, user.getUserId()));
        return AjaxResp.instance(one);
    }


    @GetMapping(value = "selectListFromCache")
    public AjaxResp selectListFromCache() {
        List<User> userList = cacheUserList.get("USER_LIST", s -> userService.list());
        return AjaxResp.instance(userList);
    }

    @GetMapping(value = "selectDetailList")
    public AjaxResp selectDetailList() {
        List<User> userList = userService.list();
        Map<Integer, List<User>> groupingUserHash = userList.stream()
                .collect(Collectors.groupingBy(item -> item.getUserId() % 2));
        return AjaxResp.instance(groupingUserHash);
    }
}
